<?php
class Scrinker_JS extends Scrinker
{
	public function __construct($options = array())
	{
		if (empty($options['template']))
		 	$options['template'] = '<script src="%s" type="text/javascript"></script>';
		parent::__construct($options);

	}

	public function after_combine($string, $options, $flags)
	{
		if (($flags & self::MINIMIZE))
		{
			// Step 1: Preprocess js via JSMin+
			$string = JSMinPlus::minify($string);

			// Step 2: If filesize is appropriate, send js to close compiler
			if (strlen($string) < 175000) # The closure compiler allows up to 200.000 byte of POST data, I added a margin
			{
				$closured_code = HTTP::POST('http://closure-compiler.appspot.com/compile', http_build_query(array(
			    	'js_code'           =>  $string,
			    	'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
					'output_format'     => 'text',
					'output_info'       => 'compiled_code',
				), '', '&'));
				if (!empty($closured_code) and strlen($closured_code) < strlen($string))
					$string = $closured_code;
			}
		}

		return parent::after_combine($string, $options, $flags);
	}
}
